<!-- src/components/Header.vue -->
<template>
  <header class="header">
    <div class="logo">四谷家政</div>
    <nav class="nav">
      <ul>
        <li><router-link to="/">首页</router-link></li>
        <Select1 :options="[
            { text: '清洁保养', link: '/service1' },
            { text: '疏通维修', link: '/service2' },
            { text: '搬家搬场', link: '/service3' },
            { text: '家务管家', link: '/service4' },
            { text: '母婴护理', link: '/service5' },
            { text: '健康与养老', link: '/service6' },
          ]" />
<!--        <li><router-link to="/order">会员中心</router-link></li>-->
        <li @click="openModal" class="pointer">会员中心</li>
        <li><LoginStatus></LoginStatus></li>
        <li v-if="isLoggedIn" @click="logout" class="pointer">退出登录</li>
      </ul>
    </nav>

    
    <ModalLogin
        :is-visible="isModalVisible"
        @close="closeModal"
        @open-register="openRegister"
    />

    <ModalRegister
        :is-visible="registerVisible"
        @close="closeRegister"
        @open-login="openLogin"
    />
  </header>
</template>

<script setup>
import {BASE_URL} from "@/util/Config.js";
import {computed, ref} from 'vue';
import Select1 from './Select1.vue';
import ModalLogin from './ModalLogin.vue';
import ModalRegister from './ModalRegister.vue';
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import LoginStatus from "@/components/LoginStatus.vue";   // 只要有了 Pinia 就能 import



function logout() {
  userStore.logout()      // 清空 token / userInfo
  router.replace('/')      // 回到首页
}
const router = useRouter()
const userStore = useUserStore()
console.log('Pinia 数据:', userStore);
/* 弹框可见性 */
const isModalVisible  = ref(false)
const registerVisible = ref(false)

/* 是否已经登录 */
const isLoggedIn = computed(() => userStore.isLoggedIn)

function openModal() {
  if (isLoggedIn.value) {
    // 直接跳转
    router.push('/order')
  } else {
    // 未登录 → 弹出登录框
    isModalVisible.value = true
  }
}




function closeModal() {
  isModalVisible.value = false;
}

function openRegister() {
  registerVisible.value = true;
  isModalVisible.value = false;
}

function closeRegister() {
  registerVisible.value = false;
}

function openLogin() {
  isModalVisible.value = true;
  registerVisible.value = false;
}
</script>

<style scoped>
/* 保持你原有的header样式不变 */
</style>
<style scoped>.navbar {
  background-color: #003366;
  color: white;
  padding: 1rem 0;
}
.nav a {
  display: block;
  padding: 8px 12px;
  line-height: 1.5;
  font-size: 1rem;
}
.nav ul {
  list-style: none;
  display: flex;
  align-items: center; /* 垂直居中 */
  gap: 20px;
  margin: 0;
  padding: 0;
}
.dropdown-menu {
  background-color: yellow; /* 测试用 */
}
.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
}

.nav-links a {
  color: white;
  text-decoration: none;
  margin-left: 20px;
  font-weight: 500;
  transition: opacity 0.3s;
}

.nav-links a:hover {
  opacity: 0.7;

}
.header {
   display: flex;
   justify-content: space-between;
   align-items: center;
   background-color: #2c3e50;
   color: white;
   padding: 15px 30px;
   font-family: Arial, sans-serif;
 }

.logo {
  font-size: 1.2rem;
  font-weight: bold;
}

.nav ul {
  list-style: none;
  display: flex;
  gap: 20px;
  margin: 0;
  padding: 0;
}

.nav li {
  position: relative;
}

.nav a,
.nav span {
  text-decoration: none;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  display: block;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background 0.3s ease;
}

.nav a:hover,
.nav span:hover {
  background-color: #1abc9c;
}

/* 下拉菜单 */
.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: white;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  min-width: 150px;
  z-index: 1000;
  border-radius: 4px;
  overflow: hidden;
}

.dropdown-menu li {
  display: block;
}

.dropdown-menu a {
  color: #333; /* 深色字体 */
  padding: 10px 15px;
  display: block;
  text-decoration: none;
}

.dropdown-menu a:hover {
  background-color: #f1f1f1;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #2c3e50;
  color: white;
  padding: 15px 30px;
  font-family: Arial, sans-serif;
}

.logo {
  font-size: 1.2rem;
  font-weight: bold;
}

.nav ul {
  list-style: none;
  display: flex;
  gap: 20px;
  margin: 0;
  padding: 0;
}

.nav li {
  position: relative;
}

.nav a,
.nav span {
  text-decoration: none;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  display: block;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background 0.3s ease;
}

.nav a:hover,
.nav span:hover {
  background-color: #1abc9c;
}
.dropdown {
  position: relative;
  cursor: pointer;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: white;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  min-width: 150px;
  z-index: 9999;
  border-radius: 4px;
  overflow: hidden;
  margin-top: 4px;
  display: block;
}

.dropdown-menu li {
  display: block;
}

.dropdown-menu a {
  color: #333;
  padding: 10px 15px;
  display: block;
  text-decoration: none;
}

.dropdown-menu a:hover {
  background-color: #f1f1f1;
}
</style>
